﻿using Choucius.Boilerplate9.Controllers;
using Choucius.Boilerplate9.InboundRubberFrameLogs;
using Choucius.Boilerplate9.OutboundRubberFrameLogs;
using Choucius.Boilerplate9.RubberFrames;
using Choucius.Boilerplate9.Web.Models.Home;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Threading.Tasks;

namespace Choucius.Boilerplate9.Web.Controllers
{
    public class RubberFrameStatisticsController : Boilerplate9ControllerBase
    {
        private readonly RubberFrameAppService _rubberFrameAppService;
        private readonly InboundRubberFrameLogAppService _inboundRubberFrameLogAppService;
        private readonly OutboundRubberFrameLogAppService _outboundRubberFrameLogAppService;

        public RubberFrameStatisticsController(RubberFrameAppService rubberFrameAppService, InboundRubberFrameLogAppService inboundRubberFrameLogAppService, OutboundRubberFrameLogAppService outboundRubberFrameLogAppService)
        {
            _rubberFrameAppService = rubberFrameAppService;
            _inboundRubberFrameLogAppService = inboundRubberFrameLogAppService;
            _outboundRubberFrameLogAppService = outboundRubberFrameLogAppService;
        }

        public async Task<IActionResult> IndexAsync()
        {
            var rubberFrame = _rubberFrameAppService.GetStatusCount();
            ViewBag.rf = rubberFrame.Total;
            ViewBag.rf1 = rubberFrame.Initialization;
            ViewBag.rf2 = rubberFrame.InPackStock;
            ViewBag.rf3 = rubberFrame.OutPackStock;
            ViewBag.rf4 = rubberFrame.ProductBinding;
            ViewBag.rf5 = rubberFrame.OilCoated;
            ViewBag.rf6 = rubberFrame.InProductStock;
            ViewBag.rf1p = rubberFrame.Total == 0 ? "0%" : (rubberFrame.Initialization * 100 / rubberFrame.Total).ToString() + "%";
            ViewBag.rf2p = rubberFrame.Total == 0 ? "0%" : (rubberFrame.InPackStock * 100 / rubberFrame.Total).ToString() + "%";
            ViewBag.rf3p = rubberFrame.Total == 0 ? "0%" : (rubberFrame.OutPackStock * 100 / rubberFrame.Total).ToString() + "%";
            ViewBag.rf4p = rubberFrame.Total == 0 ? "0%" : (rubberFrame.ProductBinding * 100 / rubberFrame.Total).ToString() + "%";
            ViewBag.rf5p = rubberFrame.Total == 0 ? "0%" : (rubberFrame.OilCoated * 100 / rubberFrame.Total).ToString() + "%";
            ViewBag.rf6p = rubberFrame.Total == 0 ? "0%" : (rubberFrame.InProductStock * 100 / rubberFrame.Total).ToString() + "%";

            var inboundRubberFrame = await _inboundRubberFrameLogAppService.GetInboundStatistics();
            var outboundRubberFrame = await _outboundRubberFrameLogAppService.GetOutboundStatistics();
            // 示例数据
            var chartData = new HomeViewModel
            {
                Labels = new List<string>(),
                InboundPlasticContainerCount = new List<int>(),
                OutboundPlasticContainerCount = new List<int>(),
                InboundRubberFrameCount = new List<int>(),
                OutboundRubberFrameCount = new List<int>()
            };
            for (int i = 6; i >= 0; i--)
            {
                string date = DateTime.Now.Date.AddDays(-i).ToShortDateString();
                chartData.Labels.Add(date);

                int inboundRubberFrameCount = 0;
                var inboundRubberFrameData = inboundRubberFrame.FirstOrDefault(x => x.TimeInterval == date);
                if (inboundRubberFrameData != null)
                {
                    inboundRubberFrameCount = inboundRubberFrameData.TotalCount;
                }
                chartData.InboundRubberFrameCount.Add(inboundRubberFrameCount);

                int outboundRubberFrameCount = 0;
                var outboundRubberFrameData = outboundRubberFrame.FirstOrDefault(x => x.TimeInterval == date);
                if (outboundRubberFrameData != null)
                {
                    outboundRubberFrameCount = outboundRubberFrameData.TotalCount;
                }
                chartData.OutboundRubberFrameCount.Add(outboundRubberFrameCount);
            }

            return View(chartData);
        }
    }
}
